/**
 * Created by IntelliJ IDEA
 *
 * @author zhengjie
 * @Date:2022/1/7 11:12
 * @Version 1.0
 */
package com.zj.ssm.service.impl;

import com.zj.ssm.mapper.AdminMapper;
import com.zj.ssm.mapper.LoginMapper;
import com.zj.ssm.mapper.PermissionMapper;
import com.zj.ssm.pojo.domain.Admin;
import com.zj.ssm.pojo.domain.Login;
import com.zj.ssm.pojo.domain.Permission;
import com.zj.ssm.result.AjaxResult;
import com.zj.ssm.service.AdminService;
import com.zj.ssm.sysconst.LoginConst;
import com.zj.ssm.util.PermissionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private HttpSession session;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public AjaxResult login(String username, String password) {
        Login login = loginMapper.isLogin(username, password);
        if (login==null){
            return AjaxResult.fail(LoginConst.ADMIN_MSG_ZHANGHAOCUOWU);
        }

        if (login.getState().equals(LoginConst.ADMIN_STATE_DONGJIE) ){
            return AjaxResult.fail(LoginConst.ADMIN_MSG_ZHANGGAODONGJIE);
        }

        if (login.getState().equals(LoginConst.ADMIN_STATE_LIZHI)){
            return AjaxResult.fail(LoginConst.ADMIN_MSG_ZHANGHAOLIZHI);
        }

        Integer iid = login.getIid();

        Admin admin = adminMapper.getAdminByID(iid);

        if (admin.getBranch().getState().equals(LoginConst.ADMIN_STATE_UN_USE)){
            return AjaxResult.fail(LoginConst.ADMIN_MSG_BUMENWEISHIYONG);
        }

        session.setAttribute(LoginConst.ADMIN_IN_SESSION,admin);

        return AjaxResult.success();

    }

    @Override
    public AjaxResult getInfo() {
    Admin admin =(Admin) session.getAttribute(LoginConst.ADMIN_IN_SESSION);

        HashMap<Object, Object> data = new HashMap<Object, Object>();
        data.put("currentAdmin",admin);

        if (admin.getRole().getState()==0){
            if (admin.getRole().getId()==1){
                List<Permission> superAdminPermission = permissionMapper.getSuperAdminPermission();
                data.put("currentPermission",superAdminPermission);
            }else {
                List<Map<String, Object>> normalAdminPermission = permissionMapper.getNormalAdminPermission(admin.getRole().getId());
                List<Map<String,Object>> haha= PermissionUtil.haha(normalAdminPermission);
                data.put("currentPermission",haha);
            }
        }

        return AjaxResult.success(data);
    }

    @Override
    public AjaxResult cipher(String password, String newPassword, String passwordNew) {

        Admin admin =(Admin) session.getAttribute(LoginConst.ADMIN_IN_SESSION);
        Integer id = admin.getId();
        Login password1 = loginMapper.password(id);

        if (password.equals( password1.getPassword())){
            if (newPassword.equals(passwordNew)){
                int i = loginMapper.newPassword(id, newPassword);
                if (i>0){
                    return AjaxResult.success();
                }
            }
        }
        return AjaxResult.fail();
    }

    @Override
    public AjaxResult getid() {
        Admin admin =(Admin) session.getAttribute(LoginConst.ADMIN_IN_SESSION);
        Integer id = admin.getId();
        return AjaxResult.success(id);
    }


}
